//
//  RegistUserInfoView.m
//  Safflower
//
//  Created by 袁亮 on 2016/12/13.
//  Copyright © 2016年 Migic_Z. All rights reserved.
//

#import "RegistUserInfoView.h"
#import "ZZRadioControl.h"

@interface RegistUserInfoView()<ZZRadioControlDelegate>

@property (nonatomic, strong) UITextField *nick_name_field;
@property (nonatomic, strong) UITextField *sign_field;

@property (nonatomic, strong) UIButton *next_step_button;

@property (nonatomic, strong) UserModel *userInfo;

@end

@implementation RegistUserInfoView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        _userInfo = [UserModel new];
        
        [self makeRegistUserInfoView];
        
    }
    return self;
}

- (void)makeRegistUserInfoView
{
    UIView *signView = [UIView new];
    signView.layer.masksToBounds = YES;
    signView.layer.borderWidth = 1;
    signView.layer.cornerRadius = 10;
    signView.layer.borderColor = [[UIColor blackColor] CGColor];
    [self addSubview:signView];
    [signView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH / 3, 36));
        make.centerY.mas_equalTo(self.mas_centerY).mas_offset(-28);
        make.centerX.mas_equalTo(self);
    }];
    
    _sign_field = [UITextField new];
    _sign_field.placeholder = @"你的目标";
    _sign_field.font = [UIFont systemFontOfSize:17.0f];
    [signView addSubview:_sign_field];
    [_sign_field mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.mas_equalTo(0);
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
    }];
    
    UIView *nickNameView = [UIView new];
    nickNameView.layer.masksToBounds = YES;
    nickNameView.layer.borderWidth = 1;
    nickNameView.layer.cornerRadius = 10;
    nickNameView.layer.borderColor = [[UIColor blackColor] CGColor];
    [self addSubview:nickNameView];
    [nickNameView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH / 3, 36));
        make.centerX.mas_equalTo(self);
        make.bottom.mas_equalTo(signView.mas_top).mas_offset(-20);
    }];
    
    _nick_name_field = [UITextField new];
    _nick_name_field.placeholder = @"昵称";
    _nick_name_field.font = [UIFont systemFontOfSize:17.0f];
    [nickNameView addSubview:_nick_name_field];
    [_nick_name_field mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.mas_equalTo(0);
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
    }];
    
    UILabel *step_title_label = [UILabel new];
    step_title_label.text = @"步骤: 2  /  3";
    step_title_label.textColor = [UIColor grayColor];
    step_title_label.textAlignment = NSTextAlignmentCenter;
    [self addSubview:step_title_label];
    [step_title_label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(nickNameView.mas_top).mas_offset(-20);
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(20);
    }];
    
    UILabel *sexLabel = [UILabel new];
    sexLabel.text = @"性    别";
    sexLabel.font = [UIFont systemFontOfSize:18.0f];
    sexLabel.textColor = [UIColor blackColor];
    [self addSubview:sexLabel];
    [sexLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(signView.mas_bottom).mas_offset(20);
        make.left.mas_equalTo(signView.mas_left).mas_offset(0);
        make.size.mas_equalTo(CGSizeMake(65, 20));
    }];
    
    UIImageView *sex_boy_image = [UIImageView new];
    sex_boy_image.image = [UIImage imageNamed:@"regist_sex_boy_image"];
    [self addSubview:sex_boy_image];
    [sex_boy_image mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(signView.mas_bottom).mas_offset(20);
        make.left.mas_equalTo(sexLabel.mas_right).mas_offset(10);
        make.size.mas_equalTo(CGSizeMake(20, 20));
    }];
    
    ZZRadioControl *boyRadio = [[ZZRadioControl alloc]initWithGroupId:@"RegistSex" index:0];
    [self addSubview:boyRadio];
    [boyRadio mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(signView.mas_bottom).mas_offset(20);
        make.left.mas_equalTo(sex_boy_image.mas_right).mas_offset(15);
        make.size.mas_equalTo(CGSizeMake(20, 20));
    }];
    
    UIImageView *sex_girl_image = [UIImageView new];
    sex_girl_image.image = [UIImage imageNamed:@"regist_sex_girl_image"];
    [self addSubview:sex_girl_image];
    [sex_girl_image mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(signView.mas_bottom).mas_offset(20);
        make.left.mas_equalTo(boyRadio.mas_right).mas_offset(30);
        make.size.mas_equalTo(CGSizeMake(20, 20));
    }];
    
    ZZRadioControl *girlRadio = [[ZZRadioControl alloc]initWithGroupId:@"RegistSex" index:1];
    [self addSubview:girlRadio];
    [girlRadio mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(signView.mas_bottom).mas_offset(20);
        make.left.mas_equalTo(sex_girl_image.mas_right).mas_offset(15);
        make.size.mas_equalTo(CGSizeMake(20, 20));
    }];
    
    [ZZRadioControl addObserverForGroupId:@"RegistSex" observer:self];
    
    _next_step_button = [UIButton new];
    [_next_step_button setTitle:@"下一步" forState:UIControlStateNormal];
    [_next_step_button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    _next_step_button.titleLabel.font = [UIFont systemFontOfSize:24.0f];
    [_next_step_button addTarget:self action:@selector(clickNextStepButton:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_next_step_button];
    [_next_step_button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(sexLabel.mas_bottom).mas_offset(20);
        make.centerX.mas_equalTo(self);
        make.size.mas_equalTo(CGSizeMake(100, 40));
    }];
}

-(void)radioButtonSelectedAtIndex:(NSUInteger)index inGroup:(NSString *)groupId{
    ZLog(@"changed to %lu in %@",(unsigned long)index,groupId);
    if (index == 0) {
        _userInfo.sex = @"1";
    }else if (index == 1) {
        _userInfo.sex = @"2";
    }
}

- (void)clickNextStepButton:(UIButton *)button
{
    _userInfo.username = _nick_name_field.text;
    _userInfo.sign = _sign_field.text;
    if ([NSString isEmptyString:_userInfo.username] == YES) {
        if ([self.delegate respondsToSelector:@selector(clickNextStepWithObject:)]) {
            [self.delegate clickNextStepWithObject:@"必须设置昵称"];
        }
    }else if ([NSString isEmptyString:_userInfo.sex] == YES){
        if ([self.delegate respondsToSelector:@selector(clickNextStepWithObject:)]) {
            [self.delegate clickNextStepWithObject:@"必须选择性别"];
        }
    }else{
        if ([self.delegate respondsToSelector:@selector(clickNextStepWithObject:)]) {
            [self.delegate clickNextStepWithObject:_userInfo];
        }
    }
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [_nick_name_field resignFirstResponder];
    [_sign_field resignFirstResponder];
}

@end
